package com.luck.emp.stockLmt.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.luck.emp.entity.EmpTStockLmt;
import com.luck.emp.entity.EmpTStockLmtId;
import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.stockLmt.logic.IStockLmtLogic;
import com.luck.emp.stockLmt.qry.StockLmtQry;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class StockLmtAction extends BaseActionSupport {
	/**
	 * 逻辑处理类
	 */
	IStockLmtLogic stockLmtLogic;
	IWhLogic whLogic;
	/**
	 * 查询对象
	 */
	StockLmtQry qry;
	/**
	 * 分页对象
	 **/
	Page page;
	/**
	 * 实体类
	 */
	EmpTStockLmt entity;
	EmpTStockLmtId id;
	List<EmpTWarehouse> whList;
	String productCodes;

	/**
	 * 列表
	 */
	public String list() throws Exception {
		UserBean userInfo = super.getUserInfo();
		whList = whLogic.findWarehouse(userInfo.getDeptId(), null);

		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (StockLmtQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new StockLmtQry();
		}
		qry.setUserInfo(userInfo);
		page = stockLmtLogic.findStockLmtPage(qry);
		return "list";
	}

	/**
	 * 新增
	 **/
	public String toAdd() throws Exception {
		UserBean userInfo = super.getUserInfo();
		whList = whLogic.findWarehouse(userInfo.getDeptId(), null);		
		entity = new EmpTStockLmt();
		super.event = "ADD";
		super.title = "新增库存限量设置";
		return "edit";
	}

	/**
	 * 修改
	 **/
	public String toEdit() throws Exception {
		UserBean userInfo = super.getUserInfo();
		whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
		entity = stockLmtLogic.get(EmpTStockLmt.class, id);
		super.event = "EDIT";
		super.title = "修改库存限量设置";
		return "edit";
	}

	/**
	 * 修改动作
	 **/
	public String edit() throws Exception {
		UserBean userInfo = super.getUserInfo();
		if ("ADD".equals(super.event)) {
			// 新增操作
			stockLmtLogic.saveStockLmt(userInfo, entity);
		} else if ("EDIT".equals(super.event)) {
			// 修改操作
			id = new EmpTStockLmtId();
			id.setProductCode(entity.getId().getProductCode());
			id.setWhCode(entity.getId().getWhCode());
			EmpTStockLmt db = stockLmtLogic.get(EmpTStockLmt.class, id);
			super.copyProperties(db, entity);
			stockLmtLogic.saveStockLmt(userInfo, db);
			super.addAttribute("qryHex", super.qryHex);
		}
		super.redirectUrl = "/emp/stockLmt_list.action";
		return super.GLOBAL_SUCCESS;
	}

	/**
	 * 删除
	 **/
	public String delete() throws Exception {
		UserBean userInfo = super.getUserInfo();
		entity = stockLmtLogic.get(EmpTStockLmt.class, id);
		this.stockLmtLogic.deleteStockLmt(userInfo, entity);
		super.renderJsonSuccess("删除成功!");
		return null;
	}

	/**
	 * 修改
	 **/
	public String view() throws Exception {
		UserBean userInfo = super.getUserInfo();
		entity = stockLmtLogic.get(EmpTStockLmt.class, id);
		super.title = "库存限量设置详细信息";
		return "view";
	}
	
	public String initList() throws Exception{
		UserBean userInfo = super.getUserInfo();
		whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (StockLmtQry) ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new StockLmtQry();
		}
		qry.setUserInfo(userInfo);
		qry.setOrgId(userInfo.getOrgId());
		if(StringUtils.isNotBlank(qry.getOperateType())){
			if(StringUtils.isBlank(qry.getWhCode())){
				super.msgStr = "必须选择仓库";
				return GLOBAL_ERROR;
			}
			page = stockLmtLogic.findStockInitPage(qry);
		}
		return "initList";
	}
	public String initStock() throws Exception{
		UserBean userInfo = super.getUserInfo();
		if(StringUtils.isBlank(qry.getWhCode()) || StringUtils.isBlank(productCodes)){
			super.msgStr = "没有选择要初始化的商品";
			return GLOBAL_ERROR;
		}
		stockLmtLogic.saveStockInit(userInfo, qry.getWhCode(), productCodes);
		super.redirectUrl = "/emp/stockLmt_initList.action";
		return super.GLOBAL_SUCCESS;
	}
	public StockLmtQry getQry() {
		return this.qry;
	}

	public void setQry(StockLmtQry qry) {
		this.qry = qry;
	}

	public IStockLmtLogic getStockLmtLogic() {
		return this.stockLmtLogic;
	}

	public void setStockLmtLogic(IStockLmtLogic stockLmtLogic) {
		this.stockLmtLogic = stockLmtLogic;
	}

	public Page getPage() {
		return this.page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public EmpTStockLmt getEntity() {
		return entity;
	}

	public void setEntity(EmpTStockLmt entity) {
		this.entity = entity;
	}

	public EmpTStockLmtId getId() {
		return id;
	}

	public void setId(EmpTStockLmtId id) {
		this.id = id;
	}

	public IWhLogic getWhLogic() {
		return whLogic;
	}

	public void setWhLogic(IWhLogic whLogic) {
		this.whLogic = whLogic;
	}

	public List<EmpTWarehouse> getWhList() {
		return whList;
	}

	public void setWhList(List<EmpTWarehouse> whList) {
		this.whList = whList;
	}

	public String getProductCodes() {
		return productCodes;
	}

	public void setProductCodes(String productCodes) {
		this.productCodes = productCodes;
	}


}